package cn.stylefeng.guns.modular.tool.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.tool.entity.ToolTaskMoney;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskMoneyParam;
import cn.stylefeng.guns.modular.tool.service.ToolTaskMoneyService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 计件明细控制器
 */
@Controller
@RequestMapping("/toolTaskMoney")
public class ToolTaskMoneyController extends BaseController {

    private String PREFIX = "/tool/toolTaskMoney";

    @Autowired
    private ToolTaskMoneyService toolTaskMoneyService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolTaskMoney.html";
    }
    
    /**
     * 跳转到计件工资页面
     */
    @RequestMapping("tofind")
    public String tofind(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolTaskMoneyfind.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolTaskMoney_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolTaskMoney_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolTaskMoneyParam toolTaskMoneyParam) {
        this.toolTaskMoneyService.add(toolTaskMoneyParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolTaskMoneyParam toolTaskMoneyParam) {
        this.toolTaskMoneyService.update(toolTaskMoneyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolTaskMoneyParam toolTaskMoneyParam) {
        this.toolTaskMoneyService.delete(toolTaskMoneyParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolTaskMoneyParam toolTaskMoneyParam) {
        ToolTaskMoney detail = this.toolTaskMoneyService.getById(toolTaskMoneyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolTaskMoneyParam toolTaskMoneyParam,String condition) {
    	User user = userService.getcurrentUser();
    	toolTaskMoneyParam.setDeptId(user.getDeptId());
        return this.toolTaskMoneyService.findPageBySpec(toolTaskMoneyParam,condition,user.getOperateId());
    }

    /**
     * 工资明细
     */
    @ResponseBody
    @RequestMapping("/listfind")
    public LayuiPageInfo listfind(ToolTaskMoneyParam toolTaskMoneyParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	toolTaskMoneyParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
    	} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
		}
        return this.toolTaskMoneyService.findPageBySpec(toolTaskMoneyParam,condition,user.getOperateId());
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/listsum")
    public LayuiPageInfo listsum(ToolTaskMoneyParam toolTaskMoneyParam,String condition) {
    	User user = userService.getcurrentUser();
    	toolTaskMoneyParam.setDeptId(user.getDeptId());
        return this.toolTaskMoneyService.findPageBySpecsum(toolTaskMoneyParam,condition,user.getOperateId());
    }

    /**
     * 工资日合计
     */
    @ResponseBody
    @RequestMapping("/listsumfind")
    public LayuiPageInfo listsumfind(ToolTaskMoneyParam toolTaskMoneyParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	toolTaskMoneyParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
    	} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
		}
        return this.toolTaskMoneyService.findPageBySpecsum(toolTaskMoneyParam,condition,user.getOperateId());
    }

    /**
     * 工资累计
     */
    @ResponseBody
    @RequestMapping("/listsumfind3")
    public LayuiPageInfo listsumfind3(ToolTaskMoneyParam toolTaskMoneyParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	toolTaskMoneyParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
    	} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
    		toolTaskMoneyParam.setV1("'"+startTime+"' and '"+endTime+"'");
		}
        return this.toolTaskMoneyService.findPageBySpecsum3(toolTaskMoneyParam,condition,user.getOperateId());
    }

}


